import emitter from "./emitter";
import playerDataInfo from "./playerData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class plane extends cc.Component {
    private plane_level = 0;//等级
    private plane_bulletAngle = 0;//子弹夹角
    private plane_attackSpeed = 0;//攻击速度
    private plane_minAngle = 0;//最小角度
    private plane_bulletNum = 0;//每次发射子弹个数
    private isStart = false;//游戏是否开始
    private bulletLevel = 4;
    private bullet_type = "B";
    private isDouble = false;

    @property(cc.Node)
    nodeBullet_left: cc.Node = null;
    @property(cc.Node)
    nodeBullet_right: cc.Node = null;
    @property(cc.Node)
    nodeBullet: cc.Node = null;
    @property(cc.Prefab)
    pfBullet: cc.Prefab = null;
    @property(cc.Prefab)
    pfJiGuang: cc.Prefab = null;

    onLoad() {
        this.initData();
        this.addEventListener()
    }

    initData() {
        this.plane_level = 1;
        this.plane_bulletAngle = 0;
        this.plane_attackSpeed = 5;
        this.plane_bulletNum = 1;
        this.plane_minAngle = 0;
        this.createBulletPool();
        //@ts-ignore
        this.changeBulletType(playerDataInfo.gameData.bulletType, true)
    }

    addEventListener() {
        emitter.on("gameStart", this.gameStart.bind(this))
        emitter.on("recycle", this.recycleBullet.bind(this));
        emitter.on("sendBullet", this.setBulletData.bind(this));
        emitter.on("changeBulletType", this.changeBulletType.bind(this));
    }

    onDisable() {
        emitter.off("gameStart", this.gameStart.bind(this))
        emitter.off("recycle", this.recycleBullet.bind(this));
        emitter.off("sendBullet", this.setBulletData.bind(this));
        emitter.off("changeBulletType", this.changeBulletType.bind(this));
    }

    //设置玩家子弹数量、最小角度和子弹角度的夹角
    setBulletData(num, minAngle, angle) {
        console.log("setBulletData-----------------------", num, minAngle, angle);
        this.plane_bulletNum = num;
        this.plane_minAngle = minAngle;
        this.plane_bulletAngle = angle;
    }

    //改变子弹类型
    changeBulletType(type, isInit = false) {
        //如果和当前类型相同等级 +1
        if (type == this.bullet_type) {
            if (Number(this.bulletLevel) + 1 <= 6) this.bulletLevel = Number(this.bulletLevel) + 1;
        }
        else {
            //等级不同重置类型和等级
            this.bullet_type = type;
            if (!isInit) this.bulletLevel = 1;
            //@ts-ignore
            else this.bulletLevel = playerDataInfo.gameData.bulletLevel;
        }

        //修改子弹类型等级
        emitter.emit("setBulletLevelAndType", this.bullet_type, this.bulletLevel);

        //是否显示两把枪
        if (type == "B" && Number(this.bulletLevel) >= 2) this.isDouble = true;
        else this.isDouble = false;


        switch (type) {
            case "A":
                this.plane_attackSpeed = 5;
                emitter.emit("setBulletLevel", this.bulletLevel);
                emitter.emit("setPlaneSpAnimation", 0);
                break;
            case "B":
                this.setBulletData(1, 0, 0);
                this.setBulletType_B();
                break;
            default:
                break;
        }
    }

    private time = 0;
    update(dt) {
        if (!this.isStart) return
        let date = Date.now() / 100;
        if (date - this.time >= this.plane_attackSpeed) {
            this.time = date;
            //@ts-ignore
            if (this.bullet_type == "B" && this.bulletLevel == 4) {
                this.sendBulletIndex -= 1;
                if (this.sendBulletIndex == 0) {
                    this.attackJiGuang();
                    return
                }
            }

            this.getBulletByPool(this.plane_bulletNum, this.plane_minAngle, this.plane_bulletAngle);
        }
        else if (this.time == 0) {
            this.time = date;
        }
    }

    //加载激光炮
    attackJiGuang() {
        this.sendBulletIndex = 40;
        let leftJG = cc.instantiate(this.pfJiGuang);
        let rightJG = cc.instantiate(this.pfJiGuang);
        let worldPosi_right = this.nodeBullet_right.parent.convertToWorldSpaceAR(this.nodeBullet_right.position);
        let nodePosi_right = this.nodeBullet.parent.convertToNodeSpaceAR(worldPosi_right);
        let worldPosi_left = this.nodeBullet_left.parent.convertToWorldSpaceAR(this.nodeBullet_left.position);
        let nodePosi_left = this.nodeBullet.parent.convertToNodeSpaceAR(worldPosi_left);
        leftJG.parent = this.nodeBullet;
        rightJG.parent = this.nodeBullet;
        rightJG.setPosition(nodePosi_right);
        leftJG.setPosition(nodePosi_left);

        this.scheduleOnce(() => {
            leftJG.destroy();
            rightJG.destroy();
        }, 1);
    }

    private sendBulletIndex;
    //根据子弹等级设置移动速度
    setBulletType_B() {
        switch (this.bulletLevel) {
            case 1:
                this.plane_attackSpeed /= 2;
                break;
            case 2:
                emitter.emit("setPlaneSpAnimation", 1);
                break;
            case 3:
                this.plane_attackSpeed /= 2;
                break;
            case 4:
                this.sendBulletIndex = 40;
                break;
            default:
                break;
        }
    }

    //创建子弹对象池
    private bulletPool;
    createBulletPool() {
        this.bulletPool = new cc.NodePool();
        for (let i = 0; i < 200; i++) {
            let bullet = cc.instantiate(this.pfBullet);
            this.bulletPool.put(bullet);
        }

        this.gameStart(true);
    }

    //当子弹和敌机发生碰撞或者超出屏幕时回收子弹
    recycleBullet(bullet: cc.Node) {
        this.bulletPool.put(bullet);
    }

    //获取子弹
    getBulletByPool(bulletNum, minAngle, bulletAngle) {
        let bullet: cc.Node = null;
        let worldPosi;
        let nodePosi;
        if (this.isDouble) bulletNum *= 2;
        for (let i = 0; i < bulletNum; i++) {
            if (this.bulletPool.size() > 0) {
                bullet = this.bulletPool.get();
            }
            else {
                let node = cc.instantiate(this.pfBullet);
                this.bulletPool.put(node);
                bullet = this.bulletPool.get();
            }
            //两把枪时创建两个子弹 一左一右
            if (this.isDouble) {
                if (i % 2 == 0) {
                    worldPosi = this.nodeBullet_left.parent.convertToWorldSpaceAR(this.nodeBullet_left.position);
                    nodePosi = this.nodeBullet.parent.convertToNodeSpaceAR(worldPosi);
                }
                else {
                    worldPosi = this.nodeBullet_right.parent.convertToWorldSpaceAR(this.nodeBullet_right.position);
                    nodePosi = this.nodeBullet.parent.convertToNodeSpaceAR(worldPosi);
                }
            }
            else {
                worldPosi = this.nodeBullet_left.parent.convertToWorldSpaceAR(this.nodeBullet_left.position);
                nodePosi = this.nodeBullet.parent.convertToNodeSpaceAR(worldPosi);
            }
            bullet.parent = this.nodeBullet;
            bullet.setPosition(nodePosi);
            bullet.children[0].setPosition(0, 0);
            if (i == 0) bullet.angle = minAngle;
            else bullet.angle = minAngle + bulletAngle * i;
            bullet.getComponent("bullet").isStop = false;
        }
    }

    //设置游戏状态
    gameStart(state) {
        this.isStart = state;
    }
}
